package com.iotek.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


import com.iotek.entity.Customer;
import com.iotek.entity.Order;
import com.iotek.entity.Payment;
import com.iotek.service.IDetailService;
import com.iotek.service.IOrderService;
import com.iotek.service.IPaymentService;

@Controller
@RequestMapping("/payment")
public class PaymentController {
	@Autowired
	private IPaymentService paymentService;
	private IOrderService orderServicer;
	private IDetailService detailService;
	
	/**
	 * 支付过程
	 * @param request
	 * @param payment
	 * @param order
	 * @param customer
	 * @return
	 */
	@RequestMapping("/paymentA.action")
	public String payment(HttpServletRequest request,Payment payment,Order order,Model model) {
		//查询订单信息
		Customer customer = (Customer)request.getSession().getAttribute("customer");
		int customerId = customer.getCustomerId();
		order.setCustomerId(customerId);
		Order o = paymentService.findOrder(order);
		String orNum = o.getOrderNumber();
		String tatol =o.getOrderTotal();
		int detailsId = o.getDetailsId();
		model.addAttribute("detailsId", detailsId);
		model.addAttribute("orNum1", orNum);
		model.addAttribute("tatol1", tatol);
		return "payment";
	}
	/**
	 * 完成支付后增加交易记录、订单状态的修改和删除相应的商品信息
	 * @param request
	 * @param payment
	 * @param order
	 * @param customer
	 * @param model
	 * @return
	 */

	@RequestMapping("/insertPayment.action")
	public String insertPayment(HttpServletRequest request,Payment payment,Order order,Customer customer) {
		//查询订单信息
	    customer = (Customer)request.getSession().getAttribute("customer");
		int customerId = customer.getCustomerId();
		String payType = request.getParameter("payType");
		String orderNumber = request.getParameter("orderNumber");
		String orderTatol = request.getParameter("orderTatol");
		String id = request.getParameter("detailsId");
		int detailsId = Integer.parseInt(id);
		payment.setCustomerId(customerId);
		payment.setOrderNumber(orderNumber);
		payment.setPaymentMoney(orderTatol);
		payment.setPaymentType(payType);
		paymentService.addPayment(payment);
		orderServicer.updateOrderState(order);
		detailService.deleteDetails(detailsId);
		return "paymentSuccess";
		
	}
	/**
	 * 删除支付记录
	 * @param request
	 * @param payment
	 * @return
	 */
	@RequestMapping("/deletePayment")
	public String deletePayment(HttpServletRequest request,Payment payment,@RequestParam("paymentId")String paymentId1) {
		int paymentId = Integer.parseInt(paymentId1);
		paymentService.deletePayment(payment);
		return "success";
		
	}
	
	/**
	 * 查询支付记录
	 * @param request
	 * @param payment
	 * @return
	 */
	@RequestMapping("/findPayment")
	public String findPament(HttpServletRequest request,Payment payment) {
		List<Payment> list = paymentService.findPayment(payment);
		request.setAttribute("Payments", list);
		return "findPayment";
	}
	
	
	
	

}
